package com.beyondprototype.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;

import java.security.Principal;

@Controller
public class GreetingController {

    private SimpMessagingTemplate template;

    @Autowired
    public GreetingController(SimpMessagingTemplate template) {
        this.template = template;
    }

    @MessageMapping("/hi")
    public String hi(HelloMessage message) throws Exception {
        template.convertAndSend("/topic/greetings","Hi, %s".formatted(message.getName()));

        return new String();
    }

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public String hello(HelloMessage message) throws Exception {
        return new String("Hello, %s".formatted(message.getName()));
    }

    @MessageMapping("/greetings")
    @SendToUser("/queue/greetings")
    public String reply(@Payload String message, Principal user) {
        return  "Hello @_@ " + message;
    }

//    @MessageExceptionHandler
//    public ApplicationError handleException(MyException exception) {
//        // ...
//        return appError;
//    }
}

